package dao;

import openConn.DruidUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CityDao {
    public static List<String> checkCity() throws SQLException {
        Connection connection = DruidUtils.openConn();
        String sql = "select city_name from city_info";
        PreparedStatement statement = connection.prepareStatement(sql);
        ResultSet resultSet = statement.executeQuery();
        List<String> cityColl = new ArrayList<>();

        while (resultSet.next()){
            String str = resultSet.getString("city_name");
            cityColl.add(str);
        }
        return cityColl;
    }
    public static String checkCityCode(String cityName) throws SQLException {
        Connection connection = DruidUtils.openConn();
        String sql = "select city_code from city_info where city_name = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1,cityName);
        ResultSet resultSet = statement.executeQuery();
        String cityCode = null;

        while (resultSet.next()){
            cityCode = resultSet.getString("city_code");

        }
        resultSet.close();
        statement.close();
        connection.close();
        return cityCode;
    }
}
